<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<div class="pro_list fr"  >
   <div class="title"  >
   	 <span class="titletxt">您现在的位置：</span>
     <ul> 
       <li class="root">会员中心</li>    
       <li>订单管理</li>    
       <li>我的购物车</li>            
     </ul>
   </div>
  <c:if test="${not empty ret }">
   	<c:if test="${ret.code ne '200'}">
   		<script type="text/javascript">alertError('${ret.result}');</script>
   		<div id=messager class="messager" >
	         <div class="contentmsg">
	             <h5>提示信息～！</h5>
	             <ul>
	                <li>${ret.result}</li>
	             </ul>
	        </div>
	     <div class="closer" onClick="showHideLayer('messager');" title="关闭"></div>
	    </div>
   	</c:if>
   </c:if>
<br/>
<div class="easyui-tabs" data-options="tools:'#tab-tools'" >
    <div title="我的购物车" style="padding:10px;">
	    <div data-options="region:'north',title:'我的购物车',split:true" >
	        <div id="tb" style="padding:2px 5px;background: #fafafa;border: 1px solid #ddd; line-height:38px;height: 38px">
	        
	        	<a href="javascript:void(0);" onclick="selectMhj();" class="easyui-linkbutton" iconCls="icon-reload" title="选择麻黄碱"  plain="true" >选择麻黄碱</a>
                <a href="javascript:void(0);" onclick="selectOther();" class="easyui-linkbutton" iconCls="icon-remove" title="选择普通药品"  plain="true" >选择普通药品</a>
	        	
	            <%--<a href="javascript:layer.msg('建设中..');" class="easyui-linkbutton" iconCls="icon-remove" title="移入隔离区" plain="true">移入隔离区</a>--%>
	            
	            
	            <div style="float:right; margin-right:50px; display:inline;">
			           	 共有商品<span style="color:#F00;font-weight: bold;" id="hejiNum"></span>件  合计金额(不含运费)： ￥<span id="hejiMoney" style="color:#F00;font-weight: bold;"></span> 
			             
			             <a href="<c:url value="/product/searchProduct.s" />" class="easyui-linkbutton" title="生成订单" style="width:100px;" >继续购物</a>
                         <a href="javascript:void(0);" class="btn-go" onclick="toOrder()" id="create_order">提交订单</a>
			             <%--<a href="<c:url value="/trade/getInfoForOrder.s" />" class="btn-go" id="create_order">提交订单</a>--%>
			    </div>
	   		 </div>
			    
		    <div data-options="region:'center',split:true" style="width: 100%;height: 460px">
                <form id="toOrderForm" action="<c:url value="/trade/getInfoForOrder.s" />" method="post"><input type="hidden" name="codes" id="codes"/></form>
				<table id="tableGrid"></table>
			</div>
		</div>
	</div>
	<!--div title="隔离区" style="padding:10px;">

			功能建设中。。。。。
	</div-->
</div>
</div>
<script>
var productsTable;

function changeCartAmount(oldAmount,newAmount,shoppingCartId,productCode){
	if(oldAmount==newAmount.value){
		return;
	}
	if(!isIntNumber(newAmount,"请输入正整数")){
		//修改数量
		var param = {"shoppingCartId":shoppingCartId,"amount":newAmount.value,"productCode":productCode};
		$.pmsAjax({
			url:'<c:url value="/shoppingCart/updateShoppingCart.s"/>',
			data:param,
			success:function(data){
				if(data.code=="200"){
					productsTable.refresh();
				}else{
					layer.msg(data.result,3, {type:8,shade: [0.2, '#ccc']});
					newAmount.value=oldAmount;
					newAmount.focus();
				}
			}
		});
	}else{
		newAmount.value=oldAmount;
		newAmount.focus();
	}
}
function actionNum(value,row,index){
	return '<input class="easyui-numberspinner" value="'+row.amount+'" style="width:60px;" data-options="increment:1" onblur="changeCartAmount('+row.amount+',this,'+row.shoppingCartId+',\''+row.productCode+'\')" />';
}

//选择麻黄碱
function selectMhj(){
    var data = $('#tableGrid').datagrid('getData');
    for(var i=0;i<data.rows.length;i++){
        if(data.rows[i].isMhj==1){
            $('#tableGrid').datagrid('selectRow',i);
        }else{
            $('#tableGrid').datagrid('unselectRow',i);
        }
    }
}

function selectOther(){
    var data = $('#tableGrid').datagrid('getData');
    for(var i=0;i<data.rows.length;i++){
        if(data.rows[i].isMhj==0){
            $('#tableGrid').datagrid('selectRow',i);
        }else{
            $('#tableGrid').datagrid('unselectRow',i);
        }
    }
}

//提交订单
function toOrder(code,i){
    var row = $('#tableGrid').datagrid('getSelections');
    if(row.length==0){
        alertWarning("您未选择任何商品!");
        return false;
    }
    var param="";
    var flag=true;
    var index=0;
    for(var i=0;i<row.length;i++){
        if(row[i].isMhj==1){
            flag=false;
            index++;
        }
        if(i>0)param+=",";
        param+=("'"+i+"':'"+row[i].productCode+"'");
    }
    if(index!=row.length&&!flag){
        alertWarning("您的订单中存在麻黄碱品种不能与其他品种混单!");
        return false;
    }

    $("#codes").val("{"+param+"}");
    $("#toOrderForm").submit();
}

function initProducts(){
	productsTable = $('#tableGrid').pDataGrid({
		 columns:[[
                  {field:'ck',checkbox:true},
	          	  {field:'productCode',title:'编码',align:'center',sortable:true,width:80,nowrap:false},
	              {field:'chsName',title:'品名',align:'center',sortable:true,width:150,nowrap:false},
	              {field:'productSpec',title:'规格',align:'center',sortable:true},
	              {field:'producingName',title:'生产厂家',align:'center',sortable:true,width:150,nowrap:false},
	              {field:'authorizeNumber',title:'批准文号',align:'center',sortable:true,width:100},
	              {field:'nowPrice',title:'价格类型',align:'center',sortable:true,width:50},
	              {field:'salePrice',title:'价格(元)',align:'center',sortable:true,width:50},
	              {field:'minPackingNumber',title:'中包装',align:'center',sortable:true,width:50},
	              {field:'packingAmount',title:'整件数',align:'center',sortable:true,width:50},
	          	  {field:'amount',title:'购买数量',formatter:actionNum,halign:'center',align:'center',width:70},
	              {field:'sumMoney',title:'汇总',align:'center',sortable:true,width:50},
	              {field:'action',title:'操作',align:'center',sortable:true,width:70,formatter:function(value,row){
						return '<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="deleteProduct(\''+row.productCode+'\')">删除</a>';
		           }}
		          ]],
		    url:'<c:url value="/shoppingCart/searchShoppingCart.s"/>',
		    pagination:true,
		    pageSize:500,
		    collapsible:true,
            rownumbers:true,
            fitColumns:true,
            checkOnSelect:true,
            autoRowHeight:true,
            singleSelect:false,
		    queryParams:{
				"productName":$('#search_productName').val(),
				"producingName":$("#search_producingName").val()
		    },
			completeCallback:function(data){
				if(data.resultCode == "100"){
					alertError(data.errorMsg);
				}else{
					$("#hejiNum").html(data.page.total);
					$("#hejiMoney").html(data.data.sumMoney);
				}
			},
			nowrap: false
	}); 
	
}
initProducts();

function deleteProduct(code){
	$.messager.confirm('确认', '您确定需要删除该条记录吗？', function(r) {
		if(r){
		var param = {"productCode":code};
		$.pmsAjax({
			url:'<c:url value="/shoppingCart/deleteShoppingCart.s"/>',
			data:param,
			success:function(data){
				if(data.code=="200"){
					layer.msg("删除成功",1, {type:1,shade: [0.2, '#ccc']});
					productsTable.refresh();
				}else{
					layer.msg(data.result,1, {type:8,shade: [0.2, '#ccc']});
				}
			}
		});
	 }
	});
}

 
</script>